﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Xml;
using System.Collections.Specialized;

namespace Xinnos.Lca.Yacht.Analyzer.View
{
    internal class ComponentRateViewModel:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public ObservableCollection<WorkBreakdownStructureViewModel> Sequences { set; get; }
        public ObservableCollection<double> Factors { get; set; }
        private double mSummary = 0;
        public double Summary
        {
            get { return mSummary; }
            set
            {
                mSummary = value;
                RaisePropertyChanged("Summary");
            }
        }

        public ComponentRateViewModel()
        {
            if (null == Sequences) Sequences = new ObservableCollection<WorkBreakdownStructureViewModel>();
            if (null == Factors)
            {
                Factors = new ObservableCollection<double>();
                Factors.CollectionChanged += OnFactorsChanged;
            }
        }

        private void Update()
        {
            double _summary = 0;
            if (0 < Factors.Count)
            {
                EnvironmentalProfilesViewModel _profiles = EnvironmentalProfilesViewModel.GetInstance();
                for (int i = 0; i < Factors.Count; i++)
                {
                    _summary += Factors[i] * _profiles.Profiles[i].Value;
                }
            }
            this.Summary = _summary;
        }

        public XmlElement Export(XmlDocument doc)
        {
            XmlElement _element = null;
            if (null != doc)
            {
                _element = doc.CreateElement("Rate");
                if (0 < this.Sequences.Count)
                {
                    XmlElement _sequences = doc.CreateElement("Sequences");
                    foreach (WorkBreakdownStructureViewModel _sequence in Sequences)
                    {
                        XmlElement _structure = doc.CreateElement("Sequence");
                        XmlAttribute _code = doc.CreateAttribute("Code");
                        _code.Value = _sequence.Code;
                        _structure.Attributes.Append(_code);
                        _sequences.AppendChild(_structure);
                    }
                    _element.AppendChild(_sequences);
                }
                if (0 < this.Factors.Count)
                {
                    XmlElement _factors = doc.CreateElement("Factors");
                    foreach (double _value in this.Factors)
                    {
                        XmlElement _factor = doc.CreateElement("Factor");
                        _factor.InnerText = _value.ToString();
                        _factors.AppendChild(_factor);
                    }
                    _element.AppendChild(_factors);
                }
            }
            return _element;
        }
        public void Import(XmlElement element)
        {
            this.Sequences.Clear();
            this.Factors.Clear();
            if (null == element) return;
            if ("Rate" != element.Name) return;
            if (element.HasChildNodes)
            {
                WorkBreakdownStructuresViewModel _structures = WorkBreakdownStructuresViewModel.GetInstance();
                foreach (XmlElement _child in element.ChildNodes)
                {
                    switch (_child.Name)
                    {
                        case "Sequences":
                            if (_child.HasChildNodes)
                            {
                                foreach (XmlElement _sequence in _child.ChildNodes)
                                {
                                    if ("Sequence" == _sequence.Name)
                                    {
                                        string _code = _sequence.Attributes["Code"].Value;
                                        Sequences.Add(_structures.FindByCode(_code));
                                    }
                                    else throw new XmlException("Sequence 요소가 잘못되었습니다.");
                                }
                            }
                            break;
                        case "Factors":
                            if (_child.HasChildNodes)
                            {
                                foreach (XmlElement _factor in _child.ChildNodes)
                                {
                                    if ("Factor" == _factor.Name)
                                    {
                                        double _value = double.Parse(_factor.InnerText);
                                        Factors.Add(_value);
                                    }
                                    else throw new XmlException("Factor 요소가 잘못되었습니다.");
                                }
                            }
                            break;
                    }
                }
            }
        }

        private void OnFactorsChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            Update();
        }
        private void RaisePropertyChanged(string strPropertyName)
        {
            PropertyChangedEventHandler _handler = this.PropertyChanged;
            if (null != _handler) _handler(this, new PropertyChangedEventArgs(strPropertyName));
        }
    }
}